<template>
  <el-dialog
    v-model="visible"
    width="800px"
    :close-on-click-modal="false"
    :before-close="handleClose"
    append-to-body
    v-loading="loading"
    class="unified-dialog edit-dialog-grouped"
  >
    <template #header>
      <DialogHeader
        icon="Edit"
        title="分组编辑表单示例"
        description="使用 form-section 和 section-title 分组，适用于需要按功能模块分组的编辑场景"
        type="primary"
      />
    </template>
    
    <el-form
      ref="formRef"
      :model="form"
      :rules="rules"
      label-width="140px"
      label-position="left"
      class="edit-form"
    >
      <!-- 基础信息分组 -->
      <div class="form-section">
        <div class="section-title">
          <el-icon><Document /></el-icon>
          <span>基础信息</span>
        </div>
        
        <el-form-item label="Topic名称" prop="topic_name">
          <el-input
            v-model="form.topic_name"
            placeholder="请输入Topic名称"
            clearable
            :disabled="true"
          />
          <span class="form-tip">Topic名称不可修改</span>
        </el-form-item>
        
        <el-form-item label="分区ID" prop="partition_id">
          <el-input-number
            v-model="form.partition_id"
            :min="-1"
            :max="999"
            :disabled="true"
            style="width: 100%"
          />
          <span class="form-tip">分区ID不可修改</span>
        </el-form-item>
      </div>
      
      <!-- 监控设置分组 -->
      <div class="form-section">
        <div class="section-title">
          <el-icon><View /></el-icon>
          <span>监控设置</span>
        </div>
        
        <el-form-item label="启用监控" prop="is_monitored">
          <div class="switch-container">
            <el-switch
              v-model="form.is_monitored"
              :active-value="1"
              :inactive-value="0"
              size="large"
            />
            <div class="switch-description">
              <p class="description-title">监控数据收集</p>
              <p class="description-text">启用后将定时收集Topic的消息数据和Offset信息</p>
            </div>
          </div>
        </el-form-item>
        
        <el-form-item label="启用告警" prop="is_alert_enabled">
          <div class="switch-container">
            <el-switch
              v-model="form.is_alert_enabled"
              :active-value="1"
              :inactive-value="0"
              size="large"
            />
            <div class="switch-description">
              <p class="description-title">消息延迟告警</p>
              <p class="description-text">启用后当消息延迟超过阈值时会发送告警通知</p>
            </div>
          </div>
        </el-form-item>
      </div>
      
      <!-- 告警配置分组 -->
      <div class="form-section">
        <div class="section-title">
          <el-icon><Bell /></el-icon>
          <span>告警配置</span>
        </div>
        
        <el-form-item label="告警阈值(小时)" prop="alert_threshold_hours">
          <el-input-number
            v-model="form.alert_threshold_hours"
            :min="1"
            :max="168"
            placeholder="请输入告警阈值"
            style="width: 200px"
          />
          <span class="form-tip">小时无新消息则触发告警</span>
        </el-form-item>
        
        <el-form-item label="Webhook URL" prop="webhook_url">
          <el-input
            v-model="form.webhook_url"
            placeholder="请输入企业微信/钉钉Webhook地址"
            clearable
          />
        </el-form-item>
        
        <el-form-item label="首次告警时间" prop="alert_first_send_time">
          <el-time-select
            v-model="form.alert_first_send_time"
            start="00:00"
            step="00:30"
            end="23:30"
            placeholder="选择首次告警发送时间"
            style="width: 200px"
          />
          <span class="form-tip">建议设置在上班时间</span>
        </el-form-item>
        
        <el-form-item label="最大告警次数" prop="max_alert_count">
          <el-input-number
            v-model="form.max_alert_count"
            :min="1"
            :max="10"
            placeholder="次"
            style="width: 200px"
          />
          <span class="form-tip">单周期内最多发送告警次数</span>
        </el-form-item>
      </div>
    </el-form>
    
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="handleClose">取消</el-button>
        <el-button type="primary" @click="handleSubmit" :loading="loading">
          保存
        </el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup>
import { ref, computed, watch } from 'vue'
import { ElMessage } from 'element-plus'
import { Document, View, Bell } from '@element-plus/icons-vue'
import DialogHeader from '@/components/DialogHeader.vue'

const props = defineProps({
  modelValue: {
    type: Boolean,
    default: false
  },
  data: {
    type: Object,
    default: () => ({})
  }
})

const emit = defineEmits(['update:modelValue', 'success'])

const visible = computed({
  get: () => props.modelValue,
  set: (value) => emit('update:modelValue', value)
})

const formRef = ref()
const loading = ref(false)

const form = ref({
  topic_name: '',
  partition_id: 0,
  is_monitored: 1,
  is_alert_enabled: 1,
  alert_threshold_hours: 24,
  webhook_url: '',
  alert_first_send_time: '09:30',
  max_alert_count: 3
})

const rules = {
  alert_threshold_hours: [
    { required: true, message: '请输入告警阈值', trigger: 'blur' },
    { type: 'number', min: 1, max: 168, message: '告警阈值必须在1-168小时之间', trigger: 'blur' }
  ],
  max_alert_count: [
    { required: true, message: '请输入最大告警次数', trigger: 'blur' },
    { type: 'number', min: 1, max: 10, message: '最大告警次数必须在1-10之间', trigger: 'blur' }
  ]
}

const handleClose = () => {
  visible.value = false
}

const handleSubmit = async () => {
  try {
    await formRef.value.validate()
    loading.value = true
    
    // 模拟保存
    await new Promise(resolve => setTimeout(resolve, 500))
    
    ElMessage.success('保存成功（演示模式）')
    emit('success')
    handleClose()
  } catch (error) {
    console.error('表单验证失败:', error)
  } finally {
    loading.value = false
  }
}

// 监听对话框显示，初始化表单
watch(visible, (newVal) => {
  if (newVal && props.data) {
    form.value = {
      topic_name: props.data.topic_name || '',
      partition_id: props.data.partition_id ?? 0,
      is_monitored: props.data.is_monitored ?? 1,
      is_alert_enabled: props.data.is_alert_enabled ?? 1,
      alert_threshold_hours: props.data.alert_threshold_hours || 24,
      webhook_url: props.data.webhook_url || '',
      alert_first_send_time: props.data.alert_first_send_time || '09:30',
      max_alert_count: props.data.max_alert_count || 3
    }
  }
})
</script>

<style scoped>
.edit-form {
  max-height: 600px;
  overflow-y: auto;
}

.form-section {
  margin-bottom: 24px;
  padding: 20px;
  background: #f8f9fa;
  border-radius: 12px;
  border: 1px solid #e4e7ed;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.05);
  transition: all 0.3s ease;
}

.form-section:hover {
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
}

.form-section:last-child {
  margin-bottom: 0;
}

.section-title {
  display: flex;
  align-items: center;
  margin-bottom: 20px;
  padding-bottom: 12px;
  border-bottom: 2px solid #409eff;
  color: #303133;
  font-size: 16px;
  font-weight: 600;
}

.section-title .el-icon {
  margin-right: 8px;
  font-size: 18px;
  color: #409eff;
  background: rgba(64, 158, 255, 0.1);
  padding: 8px;
  border-radius: 8px;
}

.form-tip {
  margin-left: 12px;
  color: #909399;
  font-size: 12px;
  line-height: 1.4;
}

.switch-container {
  display: flex;
  align-items: flex-start;
  gap: 16px;
}

.switch-description {
  flex: 1;
}

.description-title {
  margin: 0 0 4px 0;
  color: #303133;
  font-size: 14px;
  font-weight: 500;
}

.description-text {
  margin: 0;
  color: #606266;
  font-size: 12px;
  line-height: 1.4;
}
</style>

